package com.mall.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.PmsProduct;
import com.mall.model.dto.ProductDto;
import com.mall.model.vo.PageResult;
import com.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/{productId}")
    public PmsProduct findById(@PathVariable Long productId){
       PmsProduct product = productService.getOne(productId);
       return product;
    }

    /**
     * 创建商品
     * @param productDto
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody ProductDto productDto){
        productService.save(productDto);
        return CommonResult.success(null);
    }

    /**
     * 查询商品
     * @param brandId
     * @param keyword
     * @param productCategoryId
     * @param productSn
     * @param pageNum
     * @param pageSize
     * @param publishStatus
     * @param verifyStatus
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(Integer brandId,String keyword,Integer productCategoryId,String productSn,
                             Integer pageNum,Integer pageSize,Integer publishStatus,Integer verifyStatus){
       PageResult pr= productService.list(brandId,keyword,productCategoryId,productSn,pageNum,pageSize,publishStatus,verifyStatus);
       return CommonResult.success(pr);
    }

    /**
     * 根据商品名称或货号模糊查询
     * @param keyword
     * @return
     */
    @GetMapping("/simpleList")
    public CommonResult simpleList(String keyword){
    List<PmsProduct> list= productService.simpleList(keyword);
     return CommonResult.success(list);
    }

    /**
     * 批量修改删除状态
     * @param deleteStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/deleteStatus")
    public CommonResult deleteStatus(Integer deleteStatus,@RequestParam List<Integer>ids){
        productService.deleteStatus(deleteStatus,ids);
        return CommonResult.success(null);
    }

    /**
     * 批量设为新品
     * @param newStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/newStatus")
    public CommonResult newStatus(Integer newStatus,@RequestParam List<Integer>ids){
        productService.newStatus(newStatus,ids);
        return CommonResult.success(null);
    }

    /**
     * 批量上下架商品
     * @param publishStatus
     * @param ids
     * @return
     */
    @PostMapping("update/publishStatus")
    public CommonResult publishStatus(Integer publishStatus,@RequestParam List<Integer>ids){
        productService.publishStatus(publishStatus,ids);
        return CommonResult.success(null);
    }

    /**
     * 批量推荐商品
     * @param recommendStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult recommendStatus(Integer recommendStatus,@RequestParam List<Integer>ids){
        productService.recommendStatus(recommendStatus,ids);
        return CommonResult.success(null);
    }

    /**
     * 批量修改审核状态
     * @param detail
     * @param ids
     * @param verifyStatus
     * @return
     */
    @PostMapping("update/verifyStatus")
    public CommonResult verifyStatus(String detail,List<Integer>ids,Integer verifyStatus){
        productService.verifyStatus(detail,ids,verifyStatus);
        return CommonResult.success(null);
    }

    /**
     * 更新商品
     * @param id
     * @param productDto
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id,@RequestBody ProductDto productDto){
            productService.updateById(id,productDto);
            return CommonResult.success(null);
    }

    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    @GetMapping("/updateInfo/{id}")
    public CommonResult findById(@PathVariable Integer id){
       ProductDto productDto= productService.findById(id);
       return CommonResult.success(productDto);
    }
}
